package com.sdk;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

import com.com.unionpay.constant.Constant;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;

/**
 * <pre>
 *     author : liuso
 *     e-mail : xxx@xx
 *     time   : 2018/12/14
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class SpeechSynthesizerUtil {
    private static SpeechSynthesizerUtil mSpeechUtil;
    private String mEngineType;
    private SpeechSynthesizer mTts;
    private SharedPreferences mSharedPreferences;
    // 默认发音人
    private String voicer = "xiaoyan";
    private SynthesizerListener listener;

    public static SpeechSynthesizerUtil getInstance() {
        if (mSpeechUtil == null) {
            Class var0 = SpeechSynthesizerUtil.class;
            synchronized (SpeechSynthesizerUtil.class) {
                if (mSpeechUtil == null) {
                    mSpeechUtil = new SpeechSynthesizerUtil();
                }
            }
        }
        return mSpeechUtil;
    }

    public void setInitialTts(Context context) {
        this.initialTts(context, null);
    }

    public void setInitialTts(Context context, SynthesizerListener listener) {
        this.initialTts(context, listener);
    }

    private void initialTts(Context context, SynthesizerListener listener) {
        this.listener = listener;
        mTts = SpeechSynthesizer.createSynthesizer(context, mTtsInitListener);
        mSharedPreferences = context.getSharedPreferences(Constant.KEY_SHARED_PREFERENCES_NAME, context.MODE_PRIVATE);
        mEngineType = SpeechConstant.TYPE_CLOUD;
        setParam();
    }

    private void setParam() {
        // 清空参数
        mTts.setParameter(SpeechConstant.PARAMS, null);
        // 根据合成引擎设置相应参数
        if (mEngineType.equals(SpeechConstant.TYPE_CLOUD)) {
            mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
            mTts.setParameter(SpeechConstant.TTS_DATA_NOTIFY, "1");
            // 设置在线合成发音人
            mTts.setParameter(SpeechConstant.VOICE_NAME, voicer);
            //设置合成语速
            mTts.setParameter(SpeechConstant.SPEED, mSharedPreferences.getString("speed_preference", "50"));
            //设置合成音调
            mTts.setParameter(SpeechConstant.PITCH, mSharedPreferences.getString("pitch_preference", "50"));
            //设置合成音量
            mTts.setParameter(SpeechConstant.VOLUME, mSharedPreferences.getString("volume_preference", "50"));
        } else {
            mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_LOCAL);
            // 设置本地合成发音人 voicer为空，默认通过语记界面指定发音人。
            mTts.setParameter(SpeechConstant.VOICE_NAME, "");
            /**
             * TODO 本地合成不设置语速、音调、音量，默认使用语记设置
             * 开发者如需自定义参数，请参考在线合成参数设置
             */
        }
        //设置播放器音频流类型
        mTts.setParameter(SpeechConstant.STREAM_TYPE, mSharedPreferences.getString("stream_preference", "3"));
        // 设置播放合成音频打断音乐播放，默认为true
        mTts.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "true");

        // 设置音频保存路径，保存音频格式支持pcm、wav，设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
        // 注：AUDIO_FORMAT参数语记需要更新版本才能生效
        mTts.setParameter(SpeechConstant.AUDIO_FORMAT, "pcm");
        mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, Environment.getExternalStorageDirectory() + "/msc/tts.pcm");
    }

    public void speakText(String content) {
        try {
            if (this.mTts != null) {
                int result = this.mTts.startSpeaking(content, listener);
                if (result < 0) {
                    LogUtils.e("error,speaking text could not null ");
                }
            }
        } catch (Exception var3) {
            var3.printStackTrace();
        }

    }

    public void pauseSpeechSynthesizer() {
        if (this.mTts != null) {
            this.mTts.pauseSpeaking();
        }

    }

    public void stopSpeechSynthesizer() {
        if (this.mTts != null) {
            this.mTts.stopSpeaking();
        }

    }

    public void resumeSpeechSynthesizer() {
        if (this.mTts != null) {
            this.mTts.resumeSpeaking();
        }

    }

    public void releaseSpeechSynthesizer() {
        if (this.mTts != null) {
            this.mTts.destroy();
        }

    }

    public void setSpeechSynthesizerNull() {
        if (this.mTts != null) {
            this.mTts = null;
        }

    }

    public void endSpeechSynthesizer() {
        this.pauseSpeechSynthesizer();
        this.stopSpeechSynthesizer();
        this.releaseSpeechSynthesizer();
        this.setSpeechSynthesizerNull();
    }

    /**
     * 初始化监听。
     */
    private InitListener mTtsInitListener = new InitListener() {
        @Override
        public void onInit(int code) {
            LogUtils.e("InitListener init() code = " + code);
            if (code == ErrorCode.SUCCESS) {
                // 初始化成功，之后可以调用startSpeaking方法
                // 注：有的开发者在onCreate方法中创建完合成对象之后马上就调用startSpeaking进行合成，
                // 正确的做法是将onCreate中的startSpeaking调用移至这里
            }
        }
    };
}
